//
//  VCTopInfomation.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/27.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCTopInfomation: VCBase {
    
    private let titles = ["姓名","性别","邮箱","手机号","会员信息","身份证号"]
    // 布局
    private lazy var topInfomationView:ItemView = {
        let uiY = realHeight + MARGIN10
        let ui = ItemView(frame: CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: 420), count: titles.count)
        ui.isShowDetail = true
        ui.backgroundColor = UIColor.white
        ui.titleLabels = titles
        return ui
    }()
    
    // 退出登录
    private lazy var orangeBtnExitView:UIButton = {
        let ui = UIButton(title: "退出登录", name: "button_orange",radius: 5)
        let uiY = topInfomationView.frame.maxY + MARGIN40
        ui.frame = CGRect(x: MARGIN, y: uiY, width: SCREEN_WIDTH - MARGIN * 2, height: MARGIN50)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        init_date()
        init_event()
    }
}

extension VCTopInfomation {
    
    @objc func clickExit() -> Void {
        UserDefaults.standard.removeObject(forKey: SP_PASSWORD)
        NotificationCenter.default.post(name: CUSTOM_SWITCH_NOTIFICATIONCENTER, object: "exit")
    }
    
    private func init_event() -> Void {
        // 退出按钮添加点击事件
        orangeBtnExitView.addTarget(self, action: #selector(clickExit), for: .touchUpInside)
    }
    
    private func init_date() -> Void {
        guard let mobile = UserDefaults.standard.value(forKey: SP_MOBILE) else {
            return
        }
        guard let idNumber = UserDefaults.standard.value(forKey: SP_IDNUMBER) else {
            return
        }
        guard let username = UserDefaults.standard.value(forKey: SP_USERNAME) else {
            return
        }
        // 拼数据
        let dict = NSMutableDictionary()
        dict.setValue(mobile, forKey: SP_MOBILE)
        dict.setValue(idNumber, forKey: SP_IDNUMBER)
        dict.setValue(username, forKey: SP_USERNAME)
        // 请求
        var temp = [String]()
        self.doPost(api: "person/infomation", dict: dict) { (dict) in
            // 解析数据
            let result = dict.value(forKey: SP_BODYREQUEST) as! NSDictionary
            // 赋值
            temp.append(result.value(forKey: SP_USERNAME) as! String)
            temp.append(result.value(forKey: SP_GENDER) as! String)
            temp.append(result.value(forKey: SP_EMAIL) as! String)
            temp.append(result.value(forKey: SP_MOBILE) as! String)
            let permissionid = result.value(forKey: SP_PERMISSIONID) as! Int
            switch permissionid {
            case 1:
                temp.append("普通用户")
            case 2:
                temp.append("会员用户")
            case 3:
                temp.append("会员过期")
            default:
                temp.append("普通用户")
            }
            temp.append(result.value(forKey: SP_IDNUMBER) as! String)
            self.topInfomationView.detailLabels = temp
        }
        
        // 条目被点击
        topInfomationView.complete = {index in
            if index == 4 {
                self.pushNextVcHideBottomBar(vc: VCOpenMember())
                return
            }
            let vc = VCModifyInfomation()
            vc.content = temp[index]
            vc.label = self.titles[index]
            self.pushVcHideBottomBar(title: "修改个人资料", vc: vc)
        }
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(topInfomationView)
        view.addSubview(orangeBtnExitView)
    }
}
